-- bine_skill
-- create by panyl
-- 魔藤主动技能

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 威力
        local force = para[1];

        -- 1. 攻击方的魔力
        local magic = source:getMagic();

        -- 被对方百分比削弱
        local prop = PropM.combine(target, "weak_enemy", "magic");
        magic = PropM.apply(prop, magic);

        -- 被对方绝对值削弱
        prop = PropM.combine(target, "weak_enemy2", "magic");
        magic = PropM.apply(prop, magic);

        -- 4. 伤害/计算伤害时魔力+3
        local damage = math.modf(force * magic / 1000);

        -- 5. 魔法抗性
        prop = PropM.combine(target, "resist", "magic");
        -- 忽视抗性
        local p = PropM.combine(source, "ignore_resist", skillId);
        prop[3] = PropM.apply(p, prop[3]);
        p = PropM.combine(source, "ignore_resist", "magic");
        prop[3] = PropM.apply(p, prop[3]);

        damage = PropM.apply(prop, damage);

        -- 增加命中动作
        SkillM.getSequence(target):hit(source, target, skillId);

        local enemyHp = target:getHp();

        -- 伤害
        CombatM.receiveDamage(source, target, damage, skillId, extra);

        -- 如果杀死了敌人，就是吞噬掉了
        if target:isDead() then
            -- 转化为自己的
            local addHp = math.min(enemyHp, source:getMaxHp() - source:getHp());
            local hp = source:getHp() + addHp;
            source:setHp(hp);

            -- 抛出事件
            EventMgr.fire(event.CURE_BINE, { ["skillId"] = skillId, ["isAuto"] = true,});
        end

        -- 作用目标
        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 专门给魔藤用的技能，这里取魔藤对象
        local bine = source.bine;
        if not bine then
            return "";
        end

        -- 威力
        local base = para[1];

        -- 暂无加成
        local force = para[1];

        -- 2. 攻击方的魔力
        local magic = bine:getMagic();

        -- 3. 伤害
        local damageForce = math.modf(force * magic / 1000);
        local damageBase = math.modf(base * magic / 1000)
        local damageAddon = damageForce - damageBase;
        if damageAddon == 0 then
            desc = string.gsub(desc, "{damage}", damageForce);
        else
            desc = string.gsub(desc, "{damage}", damageBase .. "(+" .. damageAddon .. ")");
        end

        -- 吞噬血量
        desc = string.gsub(desc, "{absorb}", damageForce);

        return desc;
    end,
};
